﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ETL.Business.Handlers.ProjectCategoryHandler;
using ETL.Business.Handlers.ProjectTaskHandler;
using ETL.Business.Handlers.SessionHandler;
using ETL.Business.Helpers;
using ETL.Business.Models;
using ETL.Common;
using ETL.Common.Logging;
using ETL.Web.CustomCredential;
using ETL.Web.Filters;
using Microsoft.Practices.ServiceLocation;
using WebMatrix.WebData;
using ETL.Business.Handlers.ProjectETRHandler;


namespace ETL.Web.Controllers
{
    public class ETRController : Controller
    {
        //
        // GET: /ETR/

        public ActionResult Index()
        {

            // Clock on

            IEnumerable<ProjectETRModel> etrs = (new GetProjectETR()).Execute();

            // Add session to resumming previous task
            (new SetSessionResuming()).Execute("True");

            return View(etrs);
        }
        

        //For request inside the page!
        public string GetETRDetail(string etrRef)
        {
            try
            {
                if (etrRef != null)
                {
                    var etrs = (new GetProjectETR()).Execute(etrRef);

                    // Parse etr & etr description into session
                    (new SetSessionData()).Execute(Constants.SessionVars.Etr, etrs.Reference);
                    (new SetSessionData()).Execute(Constants.SessionVars.EtrDescription, etrs.Reference + "   " + etrs.Description);


                    var result = string.Empty;
                    if (etrs != null)
                    {   //NEED to convert to string
                        result += etrs.AuthorisedBy + "#";
                        result += etrs.RequestedBy + "#";
                        result += etrs.DateRequested + "#";
                        result += etrs.NoOfSheets + "#";
                        result += etrs.ReasonRequired + "#";
                        result += etrs.PerComplete + "#";
                        result += etrs.Reference + "#";
                        result += etrs.Electronics + "#";
                        result += etrs.ElectronicsHours + "#";
                        result += etrs.Software + "#";
                        result += etrs.SoftwareHours + "#";
                        result += etrs.CAD + "#";
                        result += etrs.CADHours + "#";
                        result += etrs.Other + "#";
                        result += etrs.OtherHours + "#";
                        result += etrs.Description + "#";
                        result += etrs.Requirements;
                    }
                    return result;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return string.Empty;
                throw;
            }

        }


        public void LogOnToEtr(string etrRef)
        {
            try
            {
                ProjectETRModel etrs = (new GetProjectETR()).Execute(etrRef);
                var etrStr = etrs.Reference;
                (new ClockOn()).Execute(1, etrStr, ETLCredential.UserId,ETLCredential.UserFullName, "");
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }

        }
    }
}
